///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//package managedBean.SM.test;
//
//import entity.account.Representatives;
//import entity.account.Account;
//import entity.account.Admins;
//import entity.account.Social;
//import entity.*;
//import javax.ejb.EJB;
//import javax.faces.bean.ManagedBean;
//import javax.faces.bean.RequestScoped;
//import java.util.Date;
//import session.SM.socialBean;
// 
//
//import javax.faces.application.FacesMessage;
//import javax.faces.context.FacesContext;
//import javax.faces.context.ExternalContext;
//import javax.faces.event.ActionEvent;
//import java.io.IOException;
//import java.io.Serializable;
//import java.util.List;
//import javax.faces.bean.ApplicationScoped;
//import javax.faces.bean.ViewScoped;
//import javax.servlet.http.*;
//import managedBeans.SM.NavigationBean;
//import utility.Search;
///**
// *
// * @author SeiRyuu
// */
//@ManagedBean(name = "LoginBean")
//@ApplicationScoped
//public class loginBean implements Serializable{
//    private static final long serialVersionUID = 1L;
//    @EJB
//    private socialBean socialbean;
//    private String email;
//    private String password;
//    private String firstname;
//    private String lastname;
//    private String gender;
//    private String string;
//    private Date birthdate;
//    private Boolean remember;
//    private Long id;
//    private String countryid;
//    private List<Country> country ;
//    //JY: added by me, ZL please modify accordingly
//    //private String timezone;
//    
//    private Long stakeholderid;
//    private Account acc;
//    private String type;
//    private NavigationBean nb;
//    /** Creates a new instance of loginBean */
//    public loginBean() {
//    }
//
//    public Long getId() {
//        return id;
//    }
//
//    public void setId(Long id) {
//        this.id = id;
//    }
//
//    public String getEmail() {
//        return email;
//    }
//
//    public void setEmail(String email) {
//        this.email = email;
//    }
//
//    public String getPassword() {
//        return password;
//    }
//
//    public void setPassword(String password) {
//        this.password = password;
//    }
//
//    // sign up
//    public String getFirstname() {
//        return firstname;
//    }
//
//    public void setFirstname(String firstname) {
//        this.firstname = firstname;
//    }
//
//    public String getLastname() {
//        return lastname;
//    }
//
//    public void setLastname(String lastname) {
//        this.lastname = lastname;
//    }
//
//    public Date getBirthdate() {
//
//        return birthdate;
//    }
//
//    public void setBirthdate(Date birthdate) {
//        this.birthdate = birthdate;
//    }
//
//    public String getGender() {
//
//        return gender;
//    }
//
//    public void setGender(String gender) {
//        this.gender = gender;
//    }
//
//    public Boolean getRemember() {
//        return remember;
//    }
//
//    public void setRemember(Boolean remember) {
//        this.remember = remember;
//    }
//
//    public List<Country> getCountry() {
//        return country;
//    }
//
//    public void setCountry(List<Country> country) {
//        this.country = country;
//    }
//
//    public String getCountryid() {
//        return countryid;
//    }
//
//    public void setCountryid(String countryid) {
//        this.countryid = countryid;
//    }
//
//    
//
//    public Long getStakeholderid() {
//        return stakeholderid;
//    }
//
//    public void setStakeholderid(Long stakeholderid) {
//        this.stakeholderid = stakeholderid;
//    }
//
//    public Account getAcc() {
//        return acc;
//    }
//
//    public void setAcc(Account acc) {
//        this.acc = acc;
//    }
//
//    public String getType() {
//        return type;
//    }
//
//    public void setType(String type) {
//        this.type = type;
//    }
//    
//    
//
//    public void add(ActionEvent actionEvent) {
//        // -1: existing email (verified), -2:existing email (unverified), 
//        // 0: successful, -999: server error
//System.out.println(countryid);
//        int response = socialbean.createAccount(email, password, firstname, lastname, gender, birthdate, Long.parseLong(countryid));//, timezone); //modified by JY
//        if ((response == -1) || (response == -2)) {
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Registration fail ", "There is an existing account associated with this email."));
//            password = "";
//        } else if (response == 0) {
//
//
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Registration complete ", "A confirmation email will be send to " + email + "."));
//            email = "";
//            password = "";
//            firstname = "";
//            lastname = "";
//            gender = "";
//            birthdate = null;
//            country = null;
//
//        } else if (response == -999) {
//
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Registration fail", "  Registration for new user is currently not avaliable. Please try again later. "));
//            password = "";
//        } else {
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error ", "  Server is currently having maintainence. Please try again later. "));
//
//            password = "";
//        }
//    }
//
//    public String Logout() {
//        System.out.println("Logout Successful");
//        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
//        return "login?faces-redirect=true";
//    }
//
//    public String Login() {
//
//         acc = socialbean.login(email, password);
//        
//         
//        
//        if (acc instanceof Admins) {
//            System.out.println("Login Successful");
//
//                FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
//
//                FacesContext facesContext = FacesContext.getCurrentInstance();
//            
//                 HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
//                session.setAttribute("Semail", acc.getEmail());
//                session.setAttribute("Sid", acc.getId());
//                session.setAttribute("Aid", acc.getId());
//                session.setAttribute("Password",acc.getPassword());
//        //  nb.settab("2");
//            // DO ADMIN STUFFS HERE
//            return "index?faces-redirect=true";
//        }
//        else {
//            Social me = (Social) acc;
//            System.out.print(me);
//            if (me != null) {
//                System.out.println("Login Successful");
//
//                FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
//
//                FacesContext facesContext = FacesContext.getCurrentInstance();
//               
//                HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
//                session.setAttribute("Semail", me.getEmail());
//                session.setAttribute("Sid", me.getId());
//                session.setAttribute("Password",me.getPassword());
//                  session.setAttribute("Tab", "0");
//   
//
//                System.out.println(session.getAttribute("Semail") + "logged");
//                System.out.println("session " + session.getAttribute("Semail"));
//                Cookie fsUserID = new Cookie("fsUserID", me.getId().toString());
//                Cookie fsPassword = new Cookie("fsPassword", me.getPassword());
//                if (getRemember()) {
//                    fsUserID.setMaxAge(6000000); // ~>2 months
//                    fsPassword.setMaxAge(6000000); // ~>2 months
//                } else {
//                    fsUserID.setMaxAge(-1); //not stored persistently and will be deleted when the Web browser exits
//                    fsPassword.setMaxAge(-1); //nnot stored persistently and will be deleted when the Web browser exits 
//
//                }
//                ((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(fsUserID);
//                ((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(fsPassword);
//                email = "";
//                password = "";
//                return "index?faces-redirect=true";
//
//            }
//            System.out.println("Login fail");
//            email = "";
//            password = "";
//            return "login?faces-redirect=true";
//        }
//        
//
//    }
//
//    public void checksession(ActionEvent actionEvent) {
//
//        FacesContext facesContext = FacesContext.getCurrentInstance();
//        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
//
//                acc = socialbean.validateMe((Long)session.getAttribute("Sid"), password);
// 
//        
//         
//           if (acc instanceof Representatives) {
//                type = "Representative";
//                
//            }else if (acc instanceof Admins){
//                type = "Admin";
//                
//            }else if(acc instanceof Social){
//            
//            type = "Welcome ";
//        }
//       
//          
//          
//            
//          
//           
//
//            
//         
//        
//        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "" + type+ " "+session.getAttribute("Semail") + session.getAttribute("Sid"), " logged in"));
//
//    }
//
//    public String redirect() {
//
//
//        FacesContext facesContext = FacesContext.getCurrentInstance();
//        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
//
//        if (session.getAttribute("Semail") != null) {
//
//            return "";
//            
//        } else {
//
//
//            return "login?faces-redirect=true";
//        }
//    }
//    
//     public void addadmin(ActionEvent actionEvent) {
//        // -1: existing email (verified), -2:existing email (unverified), 
//        // 0: successful, -999: server error
//
//        int response = socialbean.createAdmin(email, password, firstname, lastname, new Long(1));//, timezone); //modified by JY
//        if ((response == -1) || (response == -2)) {
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Registration fail ", "There is an existing account associated with this email."));
//            password = "";
//        } else if (response == 0) {
//
//
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Registration complete ", "A confirmation email will be send to " + email + "."));
//            email = "";
//            password = "";
//            firstname = "";
//            lastname = "";
//            gender = "";
//            birthdate = null;
//            country = null;
//
//        } else if (response == -999) {
//
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Registration fail", "  Registration for new user is currently not avaliable. Please try again later. "));
//            password = "";
//        } else {
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error ", "  Server is currently having maintainence. Please try again later. "));
//
//            password = "";
//        }
//
//
//
//
//    }
// public void addrep(ActionEvent actionEvent) {
//        // -1: existing email (verified), -2:existing email (unverified), 
//        // 0: successful, -999: server error
//   
//        int response =   socialbean.createRepresentative(email, password, firstname, lastname, new Long(1), gender, birthdate, stakeholderid);
// //modified by JY 
//        
//        System.out.println(response +"addrep");
//        if ((response == -1) || (response == -2)) {
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Registration fail ", "There is an existing account associated with this email."));
//            password = "";
//        } else if (response == 0) {
//
//
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Registration complete ", "A confirmation email will be send to " + email + "."));
//            email = "";
//
//            password = "";
//            firstname = "";
//            lastname = "";
//            gender = "";
//            birthdate = null;
//            
//            country = null;
//            stakeholderid = null;
//
//        } else if (response == -999) {
//
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Registration fail", "  Registration for new user is currently not avaliable. Please try again later. "));
//            password = "";
//        } else {
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error ", "  Server is currently having maintainence. Please try again later. "));
//
//            password = "";
//        }
//
//
//
//
//    }
// 
// public String getusername(){
//    
//      FacesContext facesContext = FacesContext.getCurrentInstance();
//               
//                HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true);
//              Long id = (Long) session.getAttribute("Sid");
//              String pw = (String) session.getAttribute("Password");
//    Account acc = socialbean.validateMe(id, pw);
//    
//              
//     return acc.getFirstName() + " " +acc.getLastName() ;
//}
//    
// public void fetchCountry(){
//    
//   this.country = socialbean.getAllCountries(); 
//    // System.out.println("JSF:" +country.get(1) + "fetch country");
// }
//    
//}
